package SVN::TeamTools::Store::Config;

use warnings;
use strict;
use Carp;
use Error qw(:try);

use XML::Simple;

use Exporter;
use Log::Log4perl qw(get_logger :levels);

use Data::Dumper;

my $config = undef;

sub hasAction {
        shift;
        my %args        = @_;
        my $action      = $args{action};
        return ("|dsp.config|" =~ /\|\Q$action\E\|/);
}

sub getTemplate {
	my $config = SVN::TeamTools::Store::Config->getconfig();
	my $authzpath = $config->{svn}->{authz};
	if ( $authzpath !~ /^\// ) {
		$authzpath = $config->{svn}->{repo} . "/" . $authzpath;
	}
	my $acl = SVN::Access->new( acl_file => $authzpath );

	my $admingroup = $acl->group('administrators');
	my $uname   = $ENV{REMOTE_USER};

	if ( $admingroup->member_exists($uname) ) {
		return HTML::Template->new( filename => 'SVN/TeamTools/Store/tmpl/config.tmpl', path => @INC );
	} else {
		return HTML::Template->new( filename => 'SVN/TeamTools/Store/tmpl/config-denied.tmpl', path => @INC );
	}
}

sub getconfig {	
	### Parse config file(s)
	my $xml = new XML::Simple;
	
	my $stc;
	try {
		$stc = $xml->XMLin("/etc/teamtools/config.xml", ForceArray => [qw(extention rules lang exclude regex treeregex context db)]);
	} otherwise {
		my $EXC = shift;
		croak "Could not open config.xml, error : $EXC";
	};
	if ( ! defined $stc ) {
		croak "Could not read config.xml";
	}
	return $stc;
}

sub new {
	my $class       = shift;

	if (! defined $config) {
		my %args        = @_;

		my $stc = SVN::TeamTools::Store::Config->getconfig();

		my %src_types;
		foreach my $lang (@{$stc->{source}->{lang}}) {
			foreach my $regex (@{$lang->{extention}}) {
				$src_types{$regex}=$lang;
			}
		}
		my $indextypes = "txt";
		while (my ($regex, $lang) = each (%src_types)) {
		        if ('1' == $lang->{index}) {
		                $indextypes="$indextypes|$regex";
		        }
		}

	        my $logger      = get_logger();
	        $logger->level($INFO);

	        my $appender    = Log::Log4perl::Appender->new(
                                "Log::Dispatch::File",
                                filename => "/var/log/teamtools/messages",
                                mode     => "append",
	        );
		my $layout 	= Log::Log4perl::Layout::PatternLayout->new(
                                                   "%d{dd/MM/yyyy HH:mm:ss} %p %C > %m{chomp}%n");
		$appender->layout ($layout);
	        $logger->add_appender ($appender);

		my $self = {
			logger		=> $logger,
			repo		=> $stc->{svn}->{repo},
			svnindex	=> $stc->{svnindex}->{path},
			depindex	=> $stc->{depindex}->{path},
			svnstats	=> $stc->{svnstats}->{path},
			reviewdb	=> $stc->{reviewrules}->{path},
			src_types	=> \%src_types,
			config		=> $stc,
			indextypes	=> $indextypes,
		};
		bless $self, $class;
		$config = $self;
		return $self;
	} else {
		return $config;
	}
}

sub getData {
	$config 	= undef;
	my $self	= shift;

	$self		= ref($self)->new();

	return $self->{config};
}
sub store {
	my $self	= shift;
	my %args        = @_;

	my $data	= $args{data};
	my $xml		= XMLout($data, SuppressEmpty=>1, NoAttr=>1, RootName=>'ToolConfig');
	my %result = (
		status => "OK"
	);
	open (my $conffile, ">", "/etc/teamtools/config.xml") or die "Can't open configuration file for update";
	print $conffile $xml;
	close $conffile;
	$self->{logger}->info("Saved new configuration: $xml");
	$self->{logger}->info("Saved new configuration: " . Dumper($data));
	$config = undef;

	return \%result;
}
1;
